//
//  XinyuInteractionViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/19.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "XinyuInteractionViewController.h"
#import "FamousTeacherViewController.h"
#import "ResultListViewController.h"
#import "XinyuInteractionItemTableViewCell.h"
#import "MegaphoneViewController.h"
#import "SelectChildViewController.h"
#import "ChildInfo.h"

@interface XinyuInteractionViewController ()
@property (nonatomic,strong) NSMutableArray *childArray;
@end

@implementation XinyuInteractionViewController
- (NSMutableArray *)childArray{
    if (!_childArray) {
        _childArray = [NSMutableArray array];
    }
    return _childArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    UIImage *image = [UIImage imageNamed:@"ins_bg"];
//    self.view.layer.contents = (id) image.CGImage;

    self.title = @"心语互动";
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XinyuInteractionItemTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"XinyuInteractionItemTableViewCell"];
    if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"1"] || [[JHUserInfo getUserInfo].role_type isEqualToString:@"2"]||[[JHUserInfo getUserInfo].role_type isEqualToString:@"3"]||[[JHUserInfo getUserInfo].role_type isEqualToString:@"4"]) {
        [self creatChildData];
    }
}
#pragma mark - 获取孩子列表
- (void)creatChildData{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppSoundGetChildList] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            
            self.childArray = [ChildInfo mj_objectArrayWithKeyValuesArray:response];
        }
    }];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XinyuInteractionItemTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"XinyuInteractionItemTableViewCell" forIndexPath:indexPath];
    cell.clipsToBounds = YES;
    cell.layer.cornerRadius = 10;
    cell.backgroundColor = KBACKGROUNDCOLOR;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSMutableArray *dataArray = [[NSMutableArray alloc]init];
    [dataArray addObject:@{@"image":@"megaphone_icon",@"title":@"传声筒",@"content":@"快来看看筒里装了什么？\n父母和孩子一起听听对方的心声，一起看看默契度吧！"}];
    [dataArray addObject:@{@"image":@"parentAdvice_icon",@"title":@"家长寄语",@"content":@"这里有家长关注孩子的目光，有成长过程中的点滴记录，每个月都有新的变化，一起见证成长的足迹"}];
    [dataArray addObject:@{@"image":@"feibook_icon",@"title":@"飞鸽传书",@"content":@"与时校园的老师在这里，等你说说烦心的事，等你聊聊疑惑的问题，等你一起畅想未来，和知心老师一起成长"}];
    NSMutableArray *allArray = [NSMutableArray arrayWithArray:[XinyuInteractionInfo mj_objectArrayWithKeyValuesArray:dataArray]];
    cell.interactionInfo = allArray[indexPath.section];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:{    //传声筒
            if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"5"]||[[JHUserInfo getUserInfo].role_type isEqualToString:@"6"]||[[JHUserInfo getUserInfo].role_type isEqualToString:@"7"]) {  //老师不能进传声筒
                [Global showActivityWithView:self.view withText:@"该功能只针对学生和家长"];
            }else if (self.childArray.count < 1){
                [Global showActivityWithView:self.view withText:[[JHUserInfo getUserInfo].role_type isEqualToString:@"1"]?@"您还没有与家长绑定":@"您还没有与孩子绑定"];
            }else{
                if (self.childArray.count > 1) {//有多个家长进选择家长页面
                    SelectChildViewController *selectChildVC = [[SelectChildViewController alloc]init];
                    selectChildVC.selectChildOrParentType = KSelectMegaphoneType;
                    selectChildVC.childArray = self.childArray;
                    [self.navigationController pushViewController:selectChildVC animated:YES];
                }else{  //有一个家长直接进传声筒页面
                    MegaphoneViewController *megaphoneVC = [[MegaphoneViewController alloc]init];
                    ChildInfo *childInfo = self.childArray[0];
                    megaphoneVC.childId = childInfo.user_id;
                    [self.navigationController pushViewController:megaphoneVC animated:YES];
                }
            }
        }
            break;
        case 1:{    //家长寄语
            if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"5"]||[[JHUserInfo getUserInfo].role_type isEqualToString:@"6"]||[[JHUserInfo getUserInfo].role_type isEqualToString:@"7"]) {  //老师不能进家长寄语
                [Global showActivityWithView:self.view withText:@"该功能只针对学生和家长"];
            }else if (self.childArray.count < 1){
                [Global showActivityWithView:self.view withText:[[JHUserInfo getUserInfo].role_type isEqualToString:@"1"]?@"您还没有与家长绑定":@"您还没有与孩子绑定"];
            }
            else{
                if (self.childArray.count > 1) {//有多个家长进选择家长页面
                    SelectChildViewController *selectChildVC = [[SelectChildViewController alloc]init];
                    selectChildVC.selectChildOrParentType = KSelectPersentType;
                    selectChildVC.childArray = self.childArray;
                    [self.navigationController pushViewController:selectChildVC animated:YES];
                }else{  //有一个家长直接进传声筒页面
                    ResultListViewController *reaultListVC = [[ResultListViewController alloc]init];
                    reaultListVC.monthListType = KPersentMonthListType;
                    ChildInfo *childInfo = self.childArray[0];
                    reaultListVC.childId = childInfo.user_id;
                    [self.navigationController pushViewController:reaultListVC animated:YES];
                }
            }
            
        }
            break;
        case 2:{    //飞鸽传书
            if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"2"]||[[JHUserInfo getUserInfo].role_type isEqualToString:@"3"]||[[JHUserInfo getUserInfo].role_type isEqualToString:@"4"]) {
                [Global showActivityWithView:self.view withText:@"该功能只针对学生和老师"];
            }else{
                FamousTeacherViewController *famousTeacherVC = [[FamousTeacherViewController alloc]init];
                [self.navigationController pushViewController:famousTeacherVC animated:YES];
            }
        }
            break;
        default:
            break;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, 10);
    //    view.backgroundColor = KBACKGROUNDCOLOR;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.01f;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
